package 面向对象_中.e多态性.练习4;

/**
 * @Projectname java_based
 * @Filename Animal
 * @Author an
 * @Data 2022/7/2 10:36
 * @Description TODO
 */
/*
 * 面试题：多态是编译时行为还是运行时行为？如何证明？
 *
 * 证明见如下：
 */
import java.util.Random;

class Animal  {

    protected void eat() {
        System.out.println("animal eat food");
    }
}

class Cat  extends Animal  {

    protected void eat() {
        System.out.println("cat eat fish");
    }
}

class Dog  extends Animal  {

    public void eat() {
        System.out.println("Dog eat bone");
    }
}

class Sheep  extends Animal  {

    public void eat() {
        System.out.println("Sheep eat grass");

    }

}

public class InterviewTest {

    public static Animal  getInstance(int key) {
        switch (key) {
            case 0:
                return new Cat ();
            case 1:
                return new Dog ();
            default:
                return new Sheep ();
        }

    }

    public static void main(String[] args) {
        int key = new Random().nextInt(3);

        System.out.println(key);

        Animal  animal = getInstance(key);

        animal.eat();

    }
}
